<?php
/**
 * 进云快速开发框架
 * @apple
 */
namespace core;
defined('BY_JYA') or exit('error');
//系统会员工具函数
class wxapp_callback{
	public function tomain($message){
		//file_put_contents(ROOT_D.'/custom/payresult/wechat_error_log.txt',ijson_encode($message)."\n",FILE_APPEND);
		if($message['Event']){
			$receive=$message['Event'];
		}else{
			$receive=$message['MsgType'];
		}
		if($receive=='funds_order_pay'){
			$order_info=$message['order_info'];
			$order_info['total_fee']=$order_info['payer_amount'];
			$this->order_pay($order_info);
		}elseif($receive=='open_product_order_pay'){
			$order_info=$message['order_info'];
			$order_info['trade_no']=$order_info['out_order_id'];
			$order_info['total_fee']=$order_info['payer_amount'];
			$this->order_pay($order_info);
		}
		return true;
	}
	public function order_pay($order_info){
		//验证订单
		$order=pdo_get('core_paylog',array('tid'=>$order_info['trade_no']));
		if(!$order){
			return false;
		}
		$payfee=$order['fee']-$order['di_fee'];
		if($payfee!=($order_info['total_fee']/100)){
			$log=$order['id'].'~'.$payfee.'~'.$order_info['total_fee']."\n";
			file_put_contents(ROOT_D.'/custom/payresult/wechat_error_log.txt',$log,FILE_APPEND);
			//exi('error order','error');
		}
		if($order['transaction_id']){
			exi('completed','SUCCESS');
		}
		$_SESSION['uid']=$order['mid'];
		//保存支付结果
		$update=array('transaction_id'=>$order_info['transaction_id'],'status'=>1,'pay_type'=>'wechat','paytime'=>time());
		//抵扣处理
		if($order['di_credit'] && $order['di_fee']>0){
			cfc('pay')->up_dipay($order);
		}
		pdo_update('core_paylog',$update,array('tid'=>$order['tid']));
		if($order['merchid']){
			$_SESSION['onemerchid']=$order['merchid'];
		}
		$shoporder=pdo_get('jy_weishop_order',array('id'=>$order['oid']));
		if($shoporder['ispay'] ==1){
			return false;
		}
		if($shoporder['price']!=$order['fee']){
			return false;
		}
		$params=array(
			'paytype'=>'wechat',
			'transid'=>$order_info['transaction_id'],
		);
		model('jy_weishop/order')->order_pay($shoporder['id'],$params);
		return true;
	}
}
?>